(function(window) {
    window.myQiniu = {
        upload: function(localFile, remoteKey, cb) {
            _wlog('to upload qiniu: ' + localFile + ' => ' + remoteKey);

            this._mod().upfile({
                file: localFile,
                name: remoteKey,
            }, function(ret, err) {
                _wlog(asJson(ret));

                if (ret.status) {
                    if (ret.oper == "complete") {
                        cb && safeFn(cb.done)(p);

                        var url = window.dynCfg.qiniuHost() + '/' + ret.info.key;
                        var p = extend(ret.info, {url: url});
                        cb && safeFn(cb.succ)(p);
                    } else if (ret.oper == "progress") {
                        cb && safeFn(cb.progress)(ret.percent);
                    } else {
                        cb && safeFn(cb.done)(p);
                        cb && safeFn(cb.fail)(ret);
                        utils.postErrNoTips(ret, 'qiniu: ' + remoteKey);
                    }
                } else {
                    cb && safeFn(cb.done)(p);
                    cb && safeFn(cb.fail)(ret);
                    utils.postErrNoTips(ret, 'qiniu: ' + remoteKey);
                }
            });
        },

        // 帖子中的图片/视频
        uploadMedia4Post: function(localFile, cb) {
            var remoteUrl = this._genKey(localFile, 'post');
            this.upload(localFile, remoteUrl, cb);
        },

        uploadAvatar: function(localFile, uid, avatarN, cb) {
            var longUid = numPadToWidth(uid, 9);
            var part1 = longUid.substr(0, 3);
            var part2 = longUid.substr(3, 2);
            var part3 = longUid.substr(5, 2);
            var part4 = longUid.substr(7, 2);

            // 后端代码中，亦有此逻辑。请不要轻易修改，一是保证前后端匹配，二是兼容老版本、老数据
            // 'qiniu_avatar/appN/nnn/nn/nn/nn_avatar_n.jpg'
            // android上，图片压缩时会将.jpg文件重命名为 .JPEG，故还是得写死为.jpg，作为key
            // var arr = ['qiniu_avatar', 'app' + cfg.app_gid, part1, part2, part3, part4 + '_avatar_' + avatarN + fileExt(localFile)];
            var arr = ['qiniu_avatar', 'app' + cfg.app_gid, part1, part2, part3, part4 + '_avatar_' + avatarN + '.jpg'];
            var key = arr.join('/');

            this.upload(localFile, key, cb);
        },

        _genKey: function(url, dir) {
            var dir2 = dir || 'misc';
            var now = new Date();
            var year = now.getFullYear();
            var month = numPadToWidth(now.getMonth() + 1, 2);
            var day = numPadToWidth(now.getDate(), 2);
            var ms = now.valueOf();
            var ext = fileExt(url);
            var rand = randomInt(4);

            // attachment/newbie/post/201811/30/1-1514569453356-2332.jpg
            return 'attachment/newbie/' + dir2 + '/' + year + month +'/' + day + '/' + app.auth.getMyId() + '-' + ms + '-' + rand + ext;
        },

        _mod: function() {
            return api.require('qiniuUpfile');
        }
    }

	function _log(o) {
		f_log('qiniu', o);
	}
	function _wlog(o) {
		f_wlog('qiniu', o);
	}
	function _elog(o) {
		f_elog('qiniu', o);
	}

})(window);
